package com.hmall.search.Web;

import com.hmall.dto.PageDTO;
import com.hmall.dto.RequestParams;
import com.hmall.pojo.Item;
import com.hmall.pojo.ItemDoc;
import com.hmall.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
@Slf4j
@RequestMapping("search")
@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;

    /**
     * 搜索
     * @param requestParam
     * @return
     * @throws IOException
     */
    @PostMapping("/list")
    public PageDTO<ItemDoc> searchList(@RequestBody RequestParams requestParam) throws IOException {
        log.info("搜索服务进入了，{}", requestParam.toString());
        PageDTO<ItemDoc> list = searchService.search(requestParam);
        log.info("搜索服务返回了");
        return list;
    }

    /**
     * 实时更新es
     * @param item
     */
    @PutMapping("/update")
    public void updateES(@RequestBody Item item){
        log.info("实时更新es：{}",item);
        try {
            searchService.updateES(item);
        } catch (IOException e) {
            log.error("-----updateES方法异常：" + e.getMessage());
        }
    }
}
